package com.yanqu.road.server.channelserver.manager.server;

import org.apache.mina.util.ConcurrentHashSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 跨地区服启动后，有连接过的区服ID
 */
public class ChannelRegisterServerMgr {

    private static final Map<Integer, Set<Long>> cross3IdMap = new ConcurrentHashMap<>();

    public static void addCross3Id(int channelId, long serverId) {
        if (!cross3IdMap.containsKey(channelId)) {
            synchronized (cross3IdMap) {
                if (!cross3IdMap.containsKey(channelId)) {
                    cross3IdMap.put(channelId, new ConcurrentHashSet<>());
                }
            }
        }
        cross3IdMap.get(channelId).add(serverId);
    }

    public static List<Long> getCross3IdList(int channelId) {
        if (cross3IdMap.containsKey(channelId)) {
            return new ArrayList<>(cross3IdMap.get(channelId));
        }
        return new ArrayList<>();
    }

    public static List<Integer> getChannelIdList() {
        return new ArrayList<>(cross3IdMap.keySet());
    }

}
